package com.hcinfos.gsoo.util;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface.OnKeyListener;
import android.text.Html;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.config.Common;
import com.hcinfos.gsoo.interaze.DialogCallBack;

public class DialogUtil {

	public static void createCallLoginDialog(final Activity activity,
			String msg, final DialogCallBack call, final String okMsg,
			final String cancelMsg) {

		LayoutInflater inflater = activity.getLayoutInflater();
		RelativeLayout dialog_rl = (RelativeLayout) inflater.inflate(
				R.layout.confirm_dialog_view, null);
		TextView hint_text = (TextView) dialog_rl
				.findViewById(R.id.confirm_dialog_hint_text);
		hint_text.setText(msg);
		final Dialog dialog = new Dialog(activity, R.style.QuitDialog);
		dialog.setContentView(dialog_rl);
		Button okBt = (Button) dialog_rl
				.findViewById(R.id.confirm_dialog_ok_btn);
		okBt.setText(okMsg);
		Button cancelBt = (Button) dialog_rl
				.findViewById(R.id.confirm_dialog_cancel_btn);
		cancelBt.setText(cancelMsg);
		okBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				call.ok();
				dialog.dismiss();

			}
		});
		cancelBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				call.cancel();
			}
		});

		Window dialogWindow = dialog.getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = (int) (280 * Common.PROPORTION); // 宽度
		lp.height = (int) (180 * Common.PROPORTION); // 高度
		dialogWindow.setAttributes(lp);
		dialog.show();
	}

	public static void createCallLoginDialog(final Activity activity,
			String msg, final DialogCallBack call, final String okMsg,
			final String cancelMsg, OnKeyListener keylistener) {

		LayoutInflater inflater = activity.getLayoutInflater();
		RelativeLayout dialog_rl = (RelativeLayout) inflater.inflate(
				R.layout.confirm_dialog_view, null);
		TextView hint_text = (TextView) dialog_rl
				.findViewById(R.id.confirm_dialog_hint_text);
		hint_text.setText(msg);
		final Dialog dialog = new Dialog(activity, R.style.QuitDialog);
		dialog.setContentView(dialog_rl);
		Button okBt = (Button) dialog_rl
				.findViewById(R.id.confirm_dialog_ok_btn);
		okBt.setText(okMsg);
		Button cancelBt = (Button) dialog_rl
				.findViewById(R.id.confirm_dialog_cancel_btn);
		cancelBt.setText(cancelMsg);
		dialog.setOnKeyListener(keylistener);
		dialog.setCanceledOnTouchOutside(false);
		okBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				call.ok();
				dialog.dismiss();

			}
		});
		cancelBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				call.cancel();
			}
		});

		Window dialogWindow = dialog.getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = (int) (280 * Common.PROPORTION); // 宽度
		lp.height = (int) (180 * Common.PROPORTION); // 高度
		dialogWindow.setAttributes(lp);
		dialog.show();
	}

	/**
	 * keybakc 不消失
	 * 
	 * @param activity
	 * @param msg
	 * @param call
	 * @param okMsg
	 * @param cancelMsg
	 */

	public static void createNoCancelDialog(final Activity activity,
			String msg, final DialogCallBack call, final String okMsg,
			final String cancelMsg, OnKeyListener keylistener) {

		LayoutInflater inflater = activity.getLayoutInflater();
		RelativeLayout dialog_rl = (RelativeLayout) inflater.inflate(
				R.layout.confirm_dialog_view, null);
		TextView hint_text = (TextView) dialog_rl
				.findViewById(R.id.confirm_dialog_hint_text);
		hint_text.setText(msg);
		final Dialog dialog = new Dialog(activity, R.style.QuitDialog);
		dialog.setContentView(dialog_rl);
		dialog.setOnKeyListener(keylistener);
		dialog.setCancelable(false);
		Button okBt = (Button) dialog_rl
				.findViewById(R.id.confirm_dialog_ok_btn);
		okBt.setText(okMsg);
		Button cancelBt = (Button) dialog_rl
				.findViewById(R.id.confirm_dialog_cancel_btn);
		cancelBt.setText(cancelMsg);
		okBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				call.ok();
				dialog.dismiss();

			}
		});
		cancelBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				call.cancel();
			}
		});

		Window dialogWindow = dialog.getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = (int) (280 * Common.PROPORTION); // 宽度
		lp.height = (int) (180 * Common.PROPORTION); // 高度
		dialogWindow.setAttributes(lp);
		dialog.show();
	}

	public static void versionDialog(final Activity activity, String msg,
			final DialogCallBack call, final String okMsg,
			final String cancelMsg) {

		LayoutInflater inflater = activity.getLayoutInflater();
		RelativeLayout dialog_rl = (RelativeLayout) inflater.inflate(
				R.layout.version_dialog_view, null);
		TextView hint_text = (TextView) dialog_rl
				.findViewById(R.id.confirm_dialog_hint_text);
		hint_text.setText(Html.fromHtml(msg));
		final Dialog dialog = new Dialog(activity, R.style.QuitDialog);
		dialog.setContentView(dialog_rl);
		Button okBt = (Button) dialog_rl
				.findViewById(R.id.confirm_dialog_ok_btn);
		okBt.setText(okMsg);
		Button cancelBt = (Button) dialog_rl
				.findViewById(R.id.confirm_dialog_cancel_btn);
		cancelBt.setText(cancelMsg);
		okBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
				call.ok();

			}
		});
		cancelBt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				call.cancel();
			}
		});

		Window dialogWindow = dialog.getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = (int) (280 * Common.PROPORTION); // 宽度
		lp.height = (int) (200 * Common.PROPORTION); // 高度
		dialogWindow.setAttributes(lp);
		dialog.show();
	}

	public static Dialog createProgressDialog(final Activity activity,
			String title, String msg) {
		LayoutInflater inflater = activity.getLayoutInflater();
		RelativeLayout dialog_rl = (RelativeLayout) inflater.inflate(
				R.layout.progress_dialog_view, null);
		TextView hint_text = (TextView) dialog_rl
				.findViewById(R.id.RequestLoadingText);
		hint_text.setText(msg);
		TextView title_text = (TextView) dialog_rl.findViewById(R.id.tip_title);
		title_text.setText(title);
		// RelativeLayout layout = (RelativeLayout) dialog_rl
		// .findViewById(R.id.loading_dialog_content_layout);

		final Dialog dialog = new Dialog(activity, R.style.QuitDialog);
		dialog.setContentView(dialog_rl);
		dialog.setCanceledOnTouchOutside(false);
		Window dialogWindow = dialog.getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = (int) (280 * Common.PROPORTION); // 宽度
		lp.height = (int) (180 * Common.PROPORTION); // 高度
		dialogWindow.setAttributes(lp);
		return dialog;
	}

	public static Dialog createWaitDialog(final Activity activity, String msg) {
		LayoutInflater inflater = activity.getLayoutInflater();
		RelativeLayout dialog_rl = (RelativeLayout) inflater.inflate(
				R.layout.progress_wait_view, null);

		TextView msg_tv = (TextView) dialog_rl
				.findViewById(R.id.RequestLoadingText);
		if (msg == null) {
			msg_tv.setText("服务连接中..");
		} else {
			msg_tv.setText(msg);
		}
		final Dialog dialog = new Dialog(activity, R.style.QuitDialog);
		dialog.setContentView(dialog_rl);
		Window dialogWindow = dialog.getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		dialogWindow.setGravity(Gravity.CENTER);
		lp.width = (int) (280 * Common.PROPORTION); // 宽度
		lp.height = (int) (180 * Common.PROPORTION); // 高度
		dialogWindow.setAttributes(lp);
		return dialog;
	}
}
